﻿using FinancialBook.Core.Service;
using FinancialBook.Models;
using MvvmCross.Core.ViewModels;
using MvvmCross.Platform;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace FinancialBook.Core.ViewModels
{
    public class CategoryViewModel : MvxViewModel
    {
        public ObservableCollection<Category> ListCategory { get; set; }
        ICategoryRepository context;

        public CategoryViewModel()
        {
            ListCategory = new ObservableCollection<Category>();
            context = Mvx.Resolve<ICategoryRepository>();

            GetAllData();
        }

        private void GetAllData()
        {
            ListCategory.Clear();
            var cats = context.AllItems();
            foreach (var item in cats)
            {
                ListCategory.Add(item);
            }
        }

        private string _categoryName;
        public string CategoryName
        {
            get { return _categoryName; }
            set
            {
                _categoryName = value;
                RaisePropertyChanged(() => CategoryName);
            }
        }

        public ICommand AddNewCategory
        {
            get
            {
                return new MvxCommand(() =>
                {
                    var category = new Category() { Name = CategoryName, Desc = string.Empty, Icon = "" };
                    context.Add(category);
                    ShowViewModel<TransactionViewModel>();
                });
            }
        }
    }
}